package com.supcon.exchangedata.service.hj;

import com.sun.org.apache.bcel.internal.generic.NEW;
import com.supcon.exchangedata.pojo.entity.oracle.CtKpiDay;
import com.supcon.exchangedata.pojo.entity.oracle.HjCarInfo;
import com.supcon.exchangedata.pojo.entity.oracle.StatistOtherParametersDay;
import com.supcon.exchangedata.pojo.entity.oracle.StatisticalRealTimeData;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.*;
import com.supcon.exchangedata.repository.master.MasterHjCarInfoRepository;
import com.supcon.exchangedata.repository.master.MasterKpiDayRepository;
import com.supcon.exchangedata.repository.master.MasterStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.master.MasterStatisticalRealTimeDataRepository;
import com.supcon.exchangedata.repository.slave1.SlaveOneStatistOtherParametersDayRepository;
import com.supcon.exchangedata.repository.slave6.*;
import com.supcon.exchangedata.service.CommonMethodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: yangbin
 * @Date: 2022/12/21/9:54
 * @Description:
 */
@Service
@Slf4j
public class HjOperationService {
    /**
     * 浙江杭州市城投集团垃圾焚烧处置量信息
     */
    @Resource
    private SlaveHjOperationInfo slaveHjOperationInfo;

    /**
     * 环境资源化处理信息
     */
    @Resource
    private SlaveHjOperationInfo1 slaveHjOperationInfo1;

    /**
     * 环境垃圾处置生产信息
     */
    @Resource
    private SlaveHjOperationInfo2 slaveHjOperationInfo2;

    /**
     * 环境垃圾日清运量信息
     */
    @Resource
    private SlaveHjOperationInfo3 slaveHjOperationInfo3;

    @Resource
    private SlaveHjOperationInfo4 slaveHjOperationInfo4;


    @Resource
    private MasterKpiDayRepository masterKpiDayRepository;

    @Resource
    private MasterStatistOtherParametersDayRepository masterStatistOtherParametersDayRepository;

    @Resource
    private CommonMethodService commonMethodService;

    @Resource
    private MasterStatisticalRealTimeDataRepository masterStatisticalRealTimeDataRepository;

    @Resource
    private SlaveHjOperationInfo6 slaveHjOperationInfo6;

    @Resource
    private SlaveHjOperationInfo7 slaveHjOperationInfo7;

    @Resource
    private SlaveHjOperationInfo8 slaveHjOperationInfo8;
    @Resource
    private SlaveHjOperationInfo5 slaveHjOperationInfo5;

    @Resource
    private MasterStatistOtherParametersDayRepository dayRepository;

    @Resource
    private SlaveHjZckcDataInfo zckcDataInfo;

    @Resource
    private SlaveHjRcwfDataInfo rcwfDataInfo;

    @Resource
    private MasterHjCarInfoRepository masterHjCarInfoRepository;

    @Resource
    private SlaveHjCleanSewageInfo slaveHjCleanSewageInfo;

    /**
     * 污水在线监测
     */
    public void hjCleanSewageNew(){
        HjCleanSewage byTop = slaveHjCleanSewageInfo.findByTop();
        if (byTop != null){

        }
    }



    /****  浙江杭州市城投集团垃圾焚烧处置量信息 start ********/

    public void hjBurnOperationData() {
        List<HjBurnDeal> topData = slaveHjOperationInfo.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjBurnDeal topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //入炉垃圾量 40038
                    String entryAmount = topDatum.getEntryAmount();
                    if (!StringUtils.isEmpty(entryAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40038L, day, entryAmount);
                    }
                    //入园垃圾量 40037
                    String parkAmount = topDatum.getParkAmount();
                    if (!StringUtils.isEmpty(parkAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40037L, day, parkAmount);
                    }
                    //垃圾坑库存量 40036
                    String stockAmount = topDatum.getStockAmount();
                    if (!StringUtils.isEmpty(stockAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40036L, day, stockAmount);
                    }
                    //飞灰产生量（吨）
                    String fhAmount = topDatum.getFhAmount();
                    if (!StringUtils.isEmpty(fhAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40085L, day, fhAmount);
                    }
                    //炉渣产生量（吨）
                    String lzAmount = topDatum.getLzAmount();
                    if (!StringUtils.isEmpty(lzAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40086L, day, lzAmount);
                    }
                }
            }
        }
    }


    public void hjBurnOperationByYearData(LocalDateTime startTime, LocalDateTime endTime) {
        List<HjBurnDeal> topData = slaveHjOperationInfo.findAllByBizTimeBetween(startTime, endTime);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjBurnDeal topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //入炉垃圾量 40038
                    String entryAmount = topDatum.getEntryAmount();
                    if (!StringUtils.isEmpty(entryAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40038L, day, entryAmount);
                    }
                    //入园垃圾量 40037
                    String parkAmount = topDatum.getParkAmount();
                    if (!StringUtils.isEmpty(parkAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40037L, day, parkAmount);
                    }
                    //垃圾坑库存量 40036
                    String stockAmount = topDatum.getStockAmount();
                    if (!StringUtils.isEmpty(stockAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40036L, day, stockAmount);
                    }
                    //飞灰产生量（吨）
                    String fhAmount = topDatum.getFhAmount();
                    if (!StringUtils.isEmpty(fhAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40085L, day, fhAmount);
                    }
                    //炉渣产生量（吨）
                    String lzAmount = topDatum.getLzAmount();
                    if (!StringUtils.isEmpty(lzAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40086L, day, lzAmount);
                    }
                }
            }
        }
    }

    /****  浙江杭州市城投集团垃圾焚烧处置量信息 end ********/

    /****  环境资源化处理信息 start ********/

    public void hjRecycleData() {
        List<HjCleanRecycle> topData = slaveHjOperationInfo1.findTopData();

        if (!CollectionUtils.isEmpty(topData)) {
            for (HjCleanRecycle topDatum : topData) {
                //天子岭厨余量
                // 天子岭餐厨量
                // 萧山环城厨余量
                // 萧山环城餐厨量
                // 象山厨余量
                // 象山餐厨量
            }
        }
    }

    /**
     * 同步沼气处理量 工业危废库存量（吨） 焚烧飞灰产生量（吨） 焚烧炉渣产生量（吨） 获取最新的一条数据
     */
    public void hjCleanRecycleNew(){
        LocalDateTime localDateTime = LocalDate.now().atStartOfDay();
        HjCleanRecycle topData = slaveHjOperationInfo1.findTopDataOne();
        if (topData!=null) {
            //查询我们数据库是否存在当天的数据
            StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11000L);
            StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType1 = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11005L);
            StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType2 = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11006L);
            //不存在
            if (distinctByDayOfMonthKeyAndObjIdAndType == null){
                saveOrUpdateStatistOtherParametersDay(4L, 11000L, topData.getDay(), topData.getGas());
            }
            if (distinctByDayOfMonthKeyAndObjIdAndType1 == null){
                saveOrUpdateStatistOtherParametersDay(4L, 11005L, topData.getDay(), topData.getKitchenWasteTzl());
            }
            if (distinctByDayOfMonthKeyAndObjIdAndType2 == null){
                saveOrUpdateStatistOtherParametersDay(4L, 11006L, topData.getDay(), topData.getGarbageKitchenTzl());
            }
        }
        HjZckcData hjZckcData = zckcDataInfo.acquireRealTimeData();
        if (hjZckcData!=null) {
            //查询我们数据库是否存在当天的数据
            StatistOtherParametersDay one1 = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11001L);
            StatistOtherParametersDay one2 = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11002L);
            StatistOtherParametersDay one3 = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11003L);
            //不存在
            if (one1 == null){
                if (hjZckcData.getNum() != null){
                    saveOrUpdateStatistOtherParametersDay(4L, 11001L, hjZckcData.getDay(), hjZckcData.getNum());
                }
            }
            if (one2 == null){
                if (hjZckcData.getFhnum() != null)
                saveOrUpdateStatistOtherParametersDay(4L, 11002L, hjZckcData.getDay(), hjZckcData.getFhnum());
            }
            if (one3 == null){
                saveOrUpdateStatistOtherParametersDay(4L, 11003L, hjZckcData.getDay(), hjZckcData.getLznum());
            }
        }
        HjRcwfData hjRcwfData = rcwfDataInfo.acquireRealTimeData();
        if (hjRcwfData!=null) {
            //查询我们数据库是否存在当天的数据
            StatistOtherParametersDay distinctByDayOfMonthKeyAndObjIdAndType = dayRepository.findDistinctByDayOfMonthKeyAndObjIdAndType(localDateTime, 4L, 11004L);
            //不存在
            if (distinctByDayOfMonthKeyAndObjIdAndType == null){
                saveOrUpdateStatistOtherParametersDay(4L, 11004L, hjRcwfData.getDay(), hjRcwfData.getYfNum());
            }
        }
    }


    /****  环境资源化处理信息 end ********/


    /**** 环境垃圾处置生产信息 start ****/
    public void hjGarbageHandleData() {
        List<HjEnvGarbageDeal> topData = slaveHjOperationInfo2.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjEnvGarbageDeal topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //处置量总量  40040  10000022260
                    String totalAmount = topDatum.getTotalAmount();
                    if (!StringUtils.isEmpty(totalAmount)) {
                        saveOrUpdateKpiDay("10000022260", day, totalAmount);
                        commonMethodService.saveRedis(totalAmount, "吨", day, "10000022260");
                        saveOrUpdateStatistOtherParametersDay(4L, 40040L, day, totalAmount);
                    }
                    // 焚烧量   40042
                    String burnAmount = topDatum.getBurnAmount();
                    if (!StringUtils.isEmpty(burnAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40042L, day, burnAmount);
                    }
                    // 厨余量   40039
                    String kitchenWasteAmount = topDatum.getKitchenWasteAmount();
                    if (!StringUtils.isEmpty(kitchenWasteAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40039L, day, kitchenWasteAmount);
                    }
                    // 餐厨量   40041
                    String garbageKitchenAmount = topDatum.getGarbageKitchenAmount();
                    if (!StringUtils.isEmpty(garbageKitchenAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40041L, day, garbageKitchenAmount);
                    }
                    // 填埋量   40043
                    String landfillAmount = topDatum.getLandfillAmount();
                    if (!StringUtils.isEmpty(landfillAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40043L, day, landfillAmount);
                    }
                }
            }

        }
    }

    public void hjGarbageHandleAllData(LocalDateTime startTime, LocalDateTime endTime) {
        List<HjEnvGarbageDeal> topData = slaveHjOperationInfo2.findAllByBizTimeBetween(startTime, endTime);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjEnvGarbageDeal topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //处置量总量  40040  10000022260
                    String totalAmount = topDatum.getTotalAmount();
                    if (!StringUtils.isEmpty(totalAmount)) {
                        saveOrUpdateKpiDay("10000022260", day, totalAmount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40040L, day, totalAmount);
                    }
                    // 焚烧量   40042
                    String burnAmount = topDatum.getBurnAmount();
                    if (!StringUtils.isEmpty(burnAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40042L, day, burnAmount);
                    }
                    // 厨余量   40039
                    String kitchenWasteAmount = topDatum.getKitchenWasteAmount();
                    if (!StringUtils.isEmpty(kitchenWasteAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40039L, day, kitchenWasteAmount);
                    }
                    // 餐厨量   40041
                    String garbageKitchenAmount = topDatum.getGarbageKitchenAmount();
                    if (!StringUtils.isEmpty(garbageKitchenAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40041L, day, garbageKitchenAmount);
                    }
                    // 填埋量   40043
                    String landfillAmount = topDatum.getLandfillAmount();
                    if (!StringUtils.isEmpty(landfillAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40043L, day, landfillAmount);
                    }
                }
            }

        }
    }

    /**** 环境垃圾处置生产信息 end ****/

    /**** 环境垃圾日清运量信息 start *****/
    public void HjGarbageCleanData() {
        List<HjGarbageCleanAll> topData = slaveHjOperationInfo5.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjGarbageCleanAll topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //清运量总量  40031   10000022259
                    String totalamount = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount)) {
                        saveOrUpdateKpiDay("10000022259", day, totalamount);
                        commonMethodService.saveRedis(totalamount, "吨", day, "10000022259");
                        saveOrUpdateStatistOtherParametersDay(4L, 40031L, day, totalamount);
                    }
                    // 九峰转运量 40032   10000022262
                    String jiufengAmount = topDatum.getJiufengAmount();
                    if (!StringUtils.isEmpty(jiufengAmount)) {
                        saveOrUpdateKpiDay("10000022262", day, jiufengAmount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40032L, day, jiufengAmount);
                    }
                    // 总厨余垃圾量  40034
                    String kitchenWasteAmount = topDatum.getKitchenWasteAmount();
                    if (!StringUtils.isEmpty(kitchenWasteAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40034L, day, kitchenWasteAmount);
                    }
                    // 总餐厨垃圾量 40035
                    String garbageKitchenAmount = topDatum.getGarbageKitchenAmount();
                    if (!StringUtils.isEmpty(garbageKitchenAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40035L, day, garbageKitchenAmount);
                    }
                    // 总其他垃圾量 40033
                    String garbageOtherAmount = topDatum.getGarbageOtherAmount();
                    if (!StringUtils.isEmpty(garbageOtherAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40033L, day, garbageOtherAmount);
                    }


                    //九峰转运车次 40056
                    String jiufengCars = topDatum.getJiufengCars();
                    if (!StringUtils.isEmpty(jiufengCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40056L, day, jiufengCars);
                    }
                    //总厨余垃圾车次 40057
                    String kitchenWasteCars = topDatum.getKitchenWasteCars();
                    if (!StringUtils.isEmpty(kitchenWasteCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40057L, day, kitchenWasteCars);
                    }
                    //总餐厨垃圾车次 40058
                    String garbageKitchenCars = topDatum.getGarbageKitchenCars();
                    if (!StringUtils.isEmpty(garbageKitchenCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40058L, day, garbageKitchenCars);
                    }
                    //总其他垃圾车次 40059
                    String garbageOtherCars = topDatum.getGarbageOtherCars();
                    if (!StringUtils.isEmpty(garbageOtherCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40059L, day, garbageOtherCars);
                    }
                    //滨江绿能垃圾量 40060
                    String bjlnAmount = topDatum.getBjlnAmount();
                    if (!StringUtils.isEmpty(bjlnAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40060L, day, bjlnAmount);
                    }
                    //滨江绿能垃圾车次 40061
                    String bjlnCars = topDatum.getBjlnCars();
                    if (!StringUtils.isEmpty(bjlnCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40061L, day, bjlnCars);
                    }
                    //萧山环城垃圾量 40062
                    String xshcAmount = topDatum.getXshcAmount();
                    if (!StringUtils.isEmpty(xshcAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40062L, day, xshcAmount);
                    }
                    //萧山环城垃圾车次 40063
                    String xshcCars = topDatum.getXshcCars();
                    if (!StringUtils.isEmpty(xshcCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40063L, day, xshcCars);
                    }

                }

            }
        }
    }


    public void hjGarbageCleanByYearData(LocalDateTime startTime, LocalDateTime endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);
        List<HjGarbageCleanAll> topData = slaveHjOperationInfo5.findAllByDayBetween(startTimeStr, endTimeStr);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjGarbageCleanAll topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //清运量总量  40031   10000022259
                    String totalamount = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount)) {
                        saveOrUpdateKpiDay("10000022259", day, totalamount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40031L, day, totalamount);
                    }
                    // 九峰转运量 40032   10000022262
                    String jiufengAmount = topDatum.getJiufengAmount();
                    if (!StringUtils.isEmpty(jiufengAmount)) {
                        saveOrUpdateKpiDay("10000022262", day, jiufengAmount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40032L, day, jiufengAmount);
                    }
                    // 总厨余垃圾量  40034
                    String kitchenWasteAmount = topDatum.getKitchenWasteAmount();
                    if (!StringUtils.isEmpty(kitchenWasteAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40034L, day, kitchenWasteAmount);
                    }
                    // 总餐厨垃圾量 40035
                    String garbageKitchenAmount = topDatum.getGarbageKitchenAmount();
                    if (!StringUtils.isEmpty(garbageKitchenAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40035L, day, garbageKitchenAmount);
                    }
                    // 总其他垃圾量 40033
                    String garbageOtherAmount = topDatum.getGarbageOtherAmount();
                    if (!StringUtils.isEmpty(garbageOtherAmount)) {

                        saveOrUpdateStatistOtherParametersDay(4L, 40033L, day, garbageOtherAmount);
                    }

                    //九峰转运车次 40056
                    String jiufengCars = topDatum.getJiufengCars();
                    if (!StringUtils.isEmpty(jiufengCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40056L, day, jiufengCars);
                    }
                    //总厨余垃圾车次 40057
                    String kitchenWasteCars = topDatum.getKitchenWasteCars();
                    if (!StringUtils.isEmpty(kitchenWasteCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40057L, day, kitchenWasteCars);
                    }
                    //总餐厨垃圾车次 40058
                    String garbageKitchenCars = topDatum.getGarbageKitchenCars();
                    if (!StringUtils.isEmpty(garbageKitchenCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40058L, day, garbageKitchenCars);
                    }
                    //总其他垃圾车次 40059
                    String garbageOtherCars = topDatum.getGarbageOtherCars();
                    if (!StringUtils.isEmpty(garbageOtherCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40059L, day, garbageOtherCars);
                    }
                    //滨江绿能垃圾量 40060
                    String bjlnAmount = topDatum.getBjlnAmount();
                    if (!StringUtils.isEmpty(bjlnAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40060L, day, bjlnAmount);
                    }
                    //滨江绿能垃圾车次 40061
                    String bjlnCars = topDatum.getBjlnCars();
                    if (!StringUtils.isEmpty(bjlnCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40061L, day, bjlnCars);
                    }
                    //萧山环城垃圾量 40062
                    String xshcAmount = topDatum.getXshcAmount();
                    if (!StringUtils.isEmpty(xshcAmount)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40062L, day, xshcAmount);
                    }
                    //萧山环城垃圾车次 40063
                    String xshcCars = topDatum.getXshcCars();
                    if (!StringUtils.isEmpty(xshcCars)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40063L, day, xshcCars);
                    }
                }

            }
        }
    }

    /**** 环境垃圾日清运量信息 end *****/

    /**** 环境垃圾日清转运信息 start *****/
    public void HjGarbageTranData() {
        List<HjGarbageTransAll> topData = slaveHjOperationInfo4.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjGarbageTransAll topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //清运量总量  40031   10000022259
                    String totalamount = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount)) {
                        saveOrUpdateKpiDay("10000022259", day, totalamount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40031L, day, totalamount);
                    }
                    //转运总量 40044
                    String totalamount1 = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40044L, day, totalamount1);
                    }
                    //转运总车次 40045
                    String cars = topDatum.getCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40045L, day, cars);
                    }
                    //天子岭转运量 40046
                    String tzlAmount = topDatum.getTzlAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40046L, day, tzlAmount);
                    }
                    //天子岭转运车次 40047
                    String cdAmount = topDatum.getTzlCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40047L, day, cdAmount);
                    }
                    //城东转运量  40048
                    String cdAmount1 = topDatum.getCdAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40048L, day, cdAmount1);
                    }
                    //城东转运车次  40049
                    String cdCars = topDatum.getCdCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40049L, day, cdCars);
                    }
                    //之江转运量  40050
                    String zjAmount = topDatum.getZjAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40050L, day, zjAmount);
                    }
                    //之江转运车次  40051
                    String zjCars = topDatum.getZjCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40051L, day, zjCars);
                    }
                }

            }
        }
    }


    //转运数据
    public void HjGarbageTranDataByYearData(LocalDateTime startTime, LocalDateTime endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);
        List<HjGarbageTransAll> topData = slaveHjOperationInfo4.findAllByDayBetween(startTimeStr, endTimeStr);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjGarbageTransAll topDatum : topData) {
                String day = topDatum.getDay();
                if (!StringUtils.isEmpty(day)) {
                    //清运量总量  40031   10000022259
                    String totalamount = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount)) {
                        saveOrUpdateKpiDay("10000022259", day, totalamount);
                        saveOrUpdateStatistOtherParametersDay(4L, 40031L, day, totalamount);
                    }
                    //转运总量 40044
                    String totalamount1 = topDatum.getTotalamount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40044L, day, totalamount1);
                    }
                    //转运总车次 40045
                    String cars = topDatum.getCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40045L, day, cars);
                    }
                    //天子岭转运量 40046
                    String tzlAmount = topDatum.getTzlAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40046L, day, tzlAmount);
                    }
                    //天子岭转运车次 40047
                    String cdAmount = topDatum.getTzlCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40047L, day, cdAmount);
                    }
                    //城东转运量  40048
                    String cdAmount1 = topDatum.getCdAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40048L, day, cdAmount1);
                    }
                    //城东转运车次  40049
                    String cdCars = topDatum.getCdCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40049L, day, cdCars);
                    }
                    //之江转运量  40050
                    String zjAmount = topDatum.getZjAmount();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40050L, day, zjAmount);
                    }
                    //之江转运车次  40051
                    String zjCars = topDatum.getZjCars();
                    if (!StringUtils.isEmpty(totalamount1)) {
                        saveOrUpdateStatistOtherParametersDay(4L, 40051L, day, zjCars);
                    }
                }

            }
        }
    }


    /**** 环境垃圾日清转运信息 start *****/


    /**** 环境垃圾日污水处理量 start *****/

    public void HjSewageTreatment() {
        List<HjSewageClean> topData = slaveHjOperationInfo7.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjSewageClean topDatum : topData) {
                String day = topDatum.getDay();
                //污水处理总量 40052
                String totalamount = topDatum.getTotalamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40052L, day, totalamount);
                }
                //污水一厂处理总量 40053
                String oneamount = topDatum.getOneamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40053L, day, oneamount);
                }
                //污水二厂处理总量 40054
                String twoamount = topDatum.getTwoamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40054L, day, twoamount);
                }
                //污水三厂处理总量 40055
                String threeamount = topDatum.getThreeamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40055L, day, threeamount);
                }
                //垃圾渗滤液 40006
                String percolateamount = topDatum.getPercolateamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40006L, day, percolateamount);
                }
                //废水产生量（吨）
                String sumamount = topDatum.getSumamount();
                if (!StringUtils.isEmpty(sumamount)){
                    saveOrUpdateStatistOtherParametersDay(4L, 40084L, day, sumamount);
                }
            }
        }

    }

    public void HjSewageTreatmentByYearData(LocalDateTime startTime, LocalDateTime endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);
        List<HjSewageClean> topData = slaveHjOperationInfo7.findAllByDayBetween(startTimeStr, endTimeStr);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjSewageClean topDatum : topData) {
                String day = topDatum.getDay();
                //污水处理总量 40052
                String totalamount = topDatum.getTotalamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40052L, day, totalamount);
                }
                //污水一厂处理总量 40053
                String oneamount = topDatum.getOneamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40053L, day, oneamount);
                }
                //污水二厂处理总量 40054
                String twoamount = topDatum.getTwoamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40054L, day, twoamount);
                }
                //污水三厂处理总量 40055
                String threeamount = topDatum.getThreeamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40055L, day, threeamount);
                }
                //垃圾渗滤液 40006
                String percolateamount = topDatum.getPercolateamount();
                if (!StringUtils.isEmpty(totalamount)) {
                    saveOrUpdateStatistOtherParametersDay(4L, 40006L, day, percolateamount);
                }
                //废水产生量（吨）
                String sumamount = topDatum.getSumamount();
                if (!StringUtils.isEmpty(sumamount)){
                    saveOrUpdateStatistOtherParametersDay(4L, 40084L, day, sumamount);
                }

            }
        }

    }

    /**** 环境垃圾日污水处理量 end *****/

    /**** 环境垃圾发电信息 start *****/
    public void garbageElectricityInfo() {
        List<HjElectricityInfo> topData = slaveHjOperationInfo8.findTopData();
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjElectricityInfo data : topData) {
                String day = data.getDay();
                BigDecimal powerGenerationTotal = BigDecimal.ZERO;
                BigDecimal electricityNetTotal = BigDecimal.ZERO;

                powerGenerationTotal = sumElectricityData(data.getTzlOneNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getTzlTwoNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getTzlThreeNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getLjsgNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getXsnyNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getXshcNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getYwNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getBjlnNum(), powerGenerationTotal);

                electricityNetTotal = sumElectricityData(data.getTzlOneNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getTzlTwoNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getTzlThreeNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getLjsgNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getXsnyNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getXshcNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getYwNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getBjlnNumNet(), electricityNetTotal);

                //年度发电量
                saveOrUpdateStatistOtherParametersDay(4L,11111L,day,data.getYearTotalAmount());

                //发电量 40008
                saveOrUpdateStatistOtherParametersDay(4L, 40008L, day, powerGenerationTotal.toPlainString());

                //上网电量 40009
                saveOrUpdateStatistOtherParametersDay(4L, 40009L, day, electricityNetTotal.toPlainString());

                //天子玲一工段发电量 40066
                saveOrUpdateStatistOtherParametersDay(4L,40066L,day,data.getTzlOneNum());

                //天子玲二工段发电量 40067
                saveOrUpdateStatistOtherParametersDay(4L,40067L,day,data.getTzlOneNum());

                //天子玲三工段发电量 40068
                saveOrUpdateStatistOtherParametersDay(4L,40068L,day,data.getTzlThreeNum());

                //天子岭一工段上网电量  40069
                saveOrUpdateStatistOtherParametersDay(4L,40069L,day,data.getTzlOneNumNet());

                //天子岭二工段上网电量 40070
                saveOrUpdateStatistOtherParametersDay(4L,40070L,day,data.getTzlTwoNumNet());

                //天子岭三工段上网电量 40071
                saveOrUpdateStatistOtherParametersDay(4L,40071L,day,data.getTzlThreeNumNet());

                //临江能源发电量 40072
                saveOrUpdateStatistOtherParametersDay(4L,40072L,day,data.getLjnyNum());

                //临江能源上网电量 40073
                saveOrUpdateStatistOtherParametersDay(4L,40073L,day,data.getLjnyNumNet());

                //临江三固发电量 40074
                saveOrUpdateStatistOtherParametersDay(4L,40074L,day,data.getLjsgNum());

                //临江三固上网电量 40075
                saveOrUpdateStatistOtherParametersDay(4L,40075L,day,data.getLjsgNumNet());

                //象山能源发电量 40076
                saveOrUpdateStatistOtherParametersDay(4L,40076L,day,data.getXsnyNum());

                //象山能源上网电量 40077
                saveOrUpdateStatistOtherParametersDay(4L,40077L,day,data.getXsnyNumNet());

                //萧山环城发电量 40078
                saveOrUpdateStatistOtherParametersDay(4L,40078L,day,data.getXshcNum());

                //萧山环城上网电量 40079
                saveOrUpdateStatistOtherParametersDay(4L,40079L,day,data.getXshcNumNet());

                //义乌发电量 40080
                saveOrUpdateStatistOtherParametersDay(4L,40080L,day,data.getYwNum());

                //义乌上网电量 40081
                saveOrUpdateStatistOtherParametersDay(4L,40081L,day,data.getYwNumNet());

                //滨江绿能发电量 40082
                saveOrUpdateStatistOtherParametersDay(4L,40082L,day,data.getBjlnNum());

                //滨江绿能上网电量 40083
                saveOrUpdateStatistOtherParametersDay(4L,40083L,day,data.getBjlnNumNet());

            }

        }
    }

    public void garbageElectricityInfoByYearData(LocalDateTime startTime, LocalDateTime endTime) {
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startTimeStr = timeFormatter.format(startTime);
        String endTimeStr = timeFormatter.format(endTime);
        List<HjElectricityInfo> topData = slaveHjOperationInfo8.findAllByDayBetween(startTimeStr, endTimeStr);
        if (!CollectionUtils.isEmpty(topData)) {
            for (HjElectricityInfo data : topData) {
                String day = data.getDay();
                BigDecimal powerGenerationTotal = BigDecimal.ZERO;
                BigDecimal electricityNetTotal = BigDecimal.ZERO;

                powerGenerationTotal = sumElectricityData(data.getTzlOneNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getTzlTwoNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getTzlThreeNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getLjsgNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getXsnyNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getXshcNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getYwNum(), powerGenerationTotal);
                powerGenerationTotal = sumElectricityData(data.getBjlnNum(), powerGenerationTotal);

                electricityNetTotal = sumElectricityData(data.getTzlOneNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getTzlTwoNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getTzlThreeNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getLjsgNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getXsnyNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getXshcNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getYwNumNet(), electricityNetTotal);
                electricityNetTotal = sumElectricityData(data.getBjlnNumNet(), electricityNetTotal);

                //发电量 40008
                saveOrUpdateStatistOtherParametersDay(4L, 40008L, day, powerGenerationTotal.toPlainString());

                //上网电量 40009
                saveOrUpdateStatistOtherParametersDay(4L, 40009L, day, electricityNetTotal.toPlainString());
            }

        }
    }

    private BigDecimal sumElectricityData(String electricityData, BigDecimal originNum) {
        if (StringUtils.isEmpty(electricityData) || "null".equals(electricityData)) {
            return originNum;
        } else {
            BigDecimal bigDecimal = new BigDecimal(electricityData);
            BigDecimal add = originNum.add(bigDecimal);
            return add;
        }
    }

    /**** 环境垃圾发电信息 end *****/


    //平均候车时长
    public void waitCarTime() {
        HjCarWaitInfo topData = slaveHjOperationInfo6.getTopData();
        DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        commonMethodService.saveRedis(topData.getMinutesnum().toString(), "分钟", timeFormatter.format(topData.getBizTime()), "10000022886");
    }
    //平均等候时长and等待车辆数
    public void waitCarTimeNew(){
        HjCarWaitInfo topData = slaveHjOperationInfo6.getTopData();
        HjCarInfo one = masterHjCarInfoRepository.findByTop(topData.getTongid());
        if (one==null){
            HjCarInfo hjCarInfo = new HjCarInfo();
            hjCarInfo.setId(topData.getTongid());
            hjCarInfo.setBizTime(topData.getBizTime());
            hjCarInfo.setMinutesnum(topData.getMinutesnum());
            hjCarInfo.setCarsnum(topData.getCarsnum());
            this.masterHjCarInfoRepository.save(hjCarInfo);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateKpiDay(String code, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        CtKpiDay byCodeAndDayOfMonthKey = masterKpiDayRepository.findByCodeAndDayOfMonthKey(code, dayOfMonthKey);
        if (byCodeAndDayOfMonthKey == null) {
            byCodeAndDayOfMonthKey = new CtKpiDay();
            byCodeAndDayOfMonthKey.setCode(code);
            byCodeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byCodeAndDayOfMonthKey.setLastUpdateTime(LocalDateTime.now());
            byCodeAndDayOfMonthKey.setDataType(code);

        }
        byCodeAndDayOfMonthKey.setCurrentValue(value);
        masterKpiDayRepository.save(byCodeAndDayOfMonthKey);
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateStatistOtherParametersDay(Long objId, Long type, String day, String value) {
        String pattern = "[0-9]*\\.?[0-9]*";
        if (StringUtils.isEmpty(value) || !value.matches(pattern)) {
            return;
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime dayOfMonthKey = LocalDate.parse(day, dateTimeFormatter).atStartOfDay();
        StatistOtherParametersDay byObjIdAndTypeAndDayOfMonthKey = masterStatistOtherParametersDayRepository.findByObjIdAndTypeAndDayOfMonthKey(objId, type, dayOfMonthKey);
        if (byObjIdAndTypeAndDayOfMonthKey == null) {
            byObjIdAndTypeAndDayOfMonthKey = new StatistOtherParametersDay();
            byObjIdAndTypeAndDayOfMonthKey.setObjId(objId);
            byObjIdAndTypeAndDayOfMonthKey.setType(type);
            byObjIdAndTypeAndDayOfMonthKey.setDayOfMonthKey(dayOfMonthKey);
            byObjIdAndTypeAndDayOfMonthKey.setObjType(1L);
            byObjIdAndTypeAndDayOfMonthKey.setLastTime(LocalDateTime.now());
            byObjIdAndTypeAndDayOfMonthKey.setInAvgValue(Double.valueOf(value));
            masterStatistOtherParametersDayRepository.save(byObjIdAndTypeAndDayOfMonthKey);
        } else {
            masterStatistOtherParametersDayRepository.updateDay(Double.valueOf(value), objId, dayOfMonthKey, type);
        }


    }

    @Transactional(rollbackFor = Exception.class)
    public void saveRealTimeData(String value, Long objId, Long dataType) {
        BigDecimal targetValue = new BigDecimal(value);
        StatisticalRealTimeData realTimeData = new StatisticalRealTimeData();
        realTimeData.setSumType(1L);
        DateTimeFormatter dateTimeFormatter1 = DateTimeFormatter.ofPattern("yyyy-MM");
        LocalDate now = LocalDate.now();
        String format1 = dateTimeFormatter1.format(now);
        realTimeData.setCurMonth(format1);
        realTimeData.setObjId(objId);
        realTimeData.setObjType(1L);
        realTimeData.setDataType(dataType);
        realTimeData.setId(0);
        realTimeData.setCurDate(now.atStartOfDay());
        realTimeData.setCurDateValue(targetValue);
        realTimeData.setLastupdatetime(LocalDateTime.now());
        masterStatisticalRealTimeDataRepository.save(realTimeData);
    }
}
